 /** 
 * The purpose of the Update Conroller is to post a transaction. There are 
 * two sperate post methods, one that finalzes the transaction and sets the
 * trasnaction to the payment and vice versa. It tehn saves it to
 * the database. The second one jsut save the transaction to the database
 * without changing the status or setting the peyment. This allows us
 * to save pending or void transactions. Another purpose of the update 
 * controller is to create an easy to follow audit trail.
 * 
 * @author      Jeff Jenkins
 * @version     1.0
 * 
 */
 
package edu.byu.isys413.jjenkins.data;

import java.sql.Date;

 /**
 * The purpose of the Update Conroller is to post a transaction. There are 
 * two sperate post methods, one that finalzes the transaction and sets the
 * trasnaction to the payment and vice versa. It tehn saves it to
 * the database. The second one jsut save the transaction to the database
 * without changing the status or setting the peyment. This allows us
 * to save pending or void transactions. Another purpose of the update 
 * controller is to create an easy to follow audit trail.
 * 
 * @author      Jeff Jenkins
 * @version     1.0
 * 
 */
public class UpdateController {
    
    /**
     * singleton variable
     */
    private static UpdateController instance = null;
    
    /** Creates a new instance of UpdateController */
    private UpdateController() {
    }
    
    
    /* This method implements the singleton pattern.
     *
     * @return: UpdateConroller instance */
    
    /**
     * singleton pattern
     * @return instance
     */
    public static synchronized UpdateController getInstance(){
        if (instance == null) {
            instance = new UpdateController();
        }
        return instance;
    }
    
    


/**
     * 
     * This method is to post transactions that are accompined by a payment. The
     * transaction is finished after the following processes
     * @param transaction to save
     * @param payment to save
     * @param curr to save
     * @throws edu.byu.isys413.jjenkins.data.DataException format
     */
    
    public void post(TransactionBO transaction, PaymentBO payment, CurrentOptsBO curr) throws DataException{
        
        // This method is called to set properties in the payment if it is a return
        if(transaction.getType().equals("Return")){
            payment.setAmount(transaction.getTotal());
            payment.setType("Return");
        }
        
        /* This method checks to see if any of the transactionlines are a backup service
         * and if they are, it will update the membership information*/
        for(int i = 0; i<transaction.getTransactionList().size(); i++){
            if(((TransactionLineBO)transaction.getTransactionList().get(i)).getRevenueSource().getType().equals("backup")){
               System.out.println("we're in");// sets backup size
                transaction.getCustomer().getMember().setBackupSize(((BackupBO)((TransactionLineBO)transaction.getTransactionList().get(i)).getRevenueSource()).getBackupSize());
                
                System.out.println("backup size is: " + ((BackupBO)((TransactionLineBO)transaction.getTransactionList().get(i)).getRevenueSource()).getBackupSize());
                // sets end date
                transaction.getCustomer().getMember().setBackupEndDate(((BackupBO)((TransactionLineBO)transaction.getTransactionList().get(i)).getRevenueSource()).getEndDate());
              
   
                CustomerDAO.getInstance().save(transaction.getCustomer());
                
            }
        }
        
        if(curr != null){
        // Set current Opts of Transaction
        transaction.setCurrentOpts(curr);
        }
        
        
        /* Set Payment in Transaction and Vice Versa
         * I choose to embed the entire objects in each other
         * because the information in the payment will almost
         * alwasy be needed when the transaction is used */
        
        transaction.setPayment(payment);
        payment.setTransaction(transaction);
        
        // Calculate Change
        transaction.setChange(payment.getAmount()- transaction.getTotal());
        
        
        
        
        // Update transaction to finished
        transaction.setStatus("finished");
        
        
        //Save Transaction
        //This payment and transactionlines
        
        TransactionDAO.getInstance().save(transaction);
        
        //Print Receipt
        transaction.display();
        
        
        
        //TODO: Update Inventory (program later)
        
        //TODO: Update Accounts Recievable (program later)
        
        //TODO: Update Cash (program later)
        
        //TODO: Process Payment with Bank (program later)
        
        //TODO: Update Customer
        
    }
    
/**
     * 
     * This method is to post transactions that are pending or void.
     * @param transaction to save
     * @param curr to save
     * @throws edu.byu.isys413.jjenkins.data.DataException formatting
     */
    public void post(TransactionBO transaction, CurrentOptsBO curr) throws DataException{
        
        
        // Set current Opts of Transaction
        transaction.setCurrentOpts(curr);
        

        
        //Save Transaction
        TransactionDAO.getInstance().save(transaction);
        
        //Print Receipt
        transaction.display();
        
        
        
        //TODO: Update Inventory (program later)
        
        //TODO: Update Accounts Recievable (program later)
        
        //TODO: Update Cash (program later)
        
        //TODO: Process Payment with Bank (program later)
        
        //TODO: Update Customer
        
    }
}
